import com.sun.jdi.ThreadReference;

import javax.swing.plaf.TableHeaderUI;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class demo9 {
    public static void main(String[] args) {
        new Ticket().start();
        new Ticket().start();
        new Ticket().start();
        new Ticket().start();

    }
}
class Ticket extends Thread{
    private static int ticket = 10;
//    private static Object obj = new Object();
    private Lock lock = new ReentrantLock();
    @Override
    public void run() {
            while(true) {
                lock.lock();
//            synchronized (obj){
                try{
                    if (ticket == 0) break;
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName() + "---卖出第" + ticket-- + "张票");
                }finally {
                    lock.unlock();
                }
//            }
            }
    }
}
